package com.ecommerce.dal.common;

import java.util.List;

import com.ecommerce.dal.entity.Category;
import com.ecommerce.dal.entity.Client;
import com.ecommerce.dal.entity.Item;
import com.ecommerce.dal.entity.ItemDetail;
import com.ecommerce.dal.service.impl.CategoryManagerService;
import com.ecommerce.dal.service.impl.ClientManagerService;
import com.ecommerce.dal.service.impl.CompanyManagerService;
import com.ecommerce.dal.service.impl.ItemManagerService;

public class TestItem {

	public TestItem(ItemManagerService itemManagerService,
			ClientManagerService clientManagerService,
			CategoryManagerService categoryManagerService) {

		Item item = new Item();

		item.setItemName("Seven Up");
		Client client = clientManagerService.retrieveClient(5L);
		Category category = categoryManagerService.retrieveCategory(5);
		ItemDetail itemDetails = new ItemDetail();

		/**insert new Item and ItemDetail*/
		item.setCategory(category);
		item.setClient(client);
		itemDetails.setExtras("Very Baeutiful and creamy crusty top");
		itemDetails.setItemSize(50);
		
		//!item.getItemDetails().add(itemDetails); //causes an error coz of lazy loading
		//must set the related item in ItemDetail before saving
		itemDetails.setItem(item);
		
		//these 2 lines are important to save items' details
		itemManagerService.saveItem(item);
		itemManagerService.saveItemDetails(itemDetails);
		
		/**updating*/		
		Item item2 = itemManagerService.retrieveItem(14L);
		System.out.println("Item Name: " + item2.getItemName());
		itemDetails.setExtras("Very Baeutiful and creamy crusty top");
		itemDetails.setItemSize(500);
		itemDetails.setItem(item2);
		

		item2.setItemPrice(2.5);
		item2.setItemDiscount(0.1);
		item2.setTotalItemPrice(item2.getItemPrice()
				* (1 - (item2.getItemDiscount() / 100)));

		
		itemDetails.setItem(item2);
		itemManagerService.updateItem(item2);
		itemManagerService.saveItemDetails(itemDetails);

		System.out.println("Item Name: " + item2.getItemName()
				+ " , Item Total Price: " + item2.getTotalItemPrice());
		System.out.println("----------- Item By Category -------");
		List<Item> itemListByCategory = itemManagerService
				.retrieveItemByCategory(6);
		for (Item itemX : itemListByCategory) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Total Price: " + itemX.getTotalItemPrice()
					+ " 		|");
		}
		System.out.println("-------------------------------------");
		System.out.println("\n----------- Item By Client Id -------");
		List<Item> itemListByClient = itemManagerService
				.retrieveItemByClient(5);
		for (Item itemX : itemListByClient) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Total Price: " + itemX.getTotalItemPrice()
					+ "  		|");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Item By Client Name -------");
		List<Item> itemListByClientName = itemManagerService
				.retrieveItemByClient("belal4");
		for (Item itemX : itemListByClientName) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ " 	 	|  Item Total Price: " + itemX.getTotalItemPrice()
					+ "  		|");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Discounted Items -------");
		List<Item> itemListDiscounted = itemManagerService
				.retrieveDiscountedItems();
		for (Item itemX : itemListDiscounted) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Total Price: " + itemX.getTotalItemPrice()
					+ "  		|  Item Price before Discount: "
					+ itemX.getItemPrice() + "  |  Item Discount: "
					+ itemX.getItemDiscount() + "  |");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Item By Discount -------");
		List<Item> itemListByDiscount = itemManagerService
				.retrieveItemByDiscount(1.0, 7.0);
		for (Item itemX : itemListByDiscount) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Discount: " + itemX.getItemDiscount()
					+ "  |");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Item By Company Id -------");
		List<Item> itemListByCompanyId = itemManagerService
				.retrieveItemByCompany(1);
		for (Item itemX : itemListByCompanyId) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Total Price: " + itemX.getTotalItemPrice()
					+ "  		|");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Item By Price -------");
		List<Item> itemListByPrice = itemManagerService.retrieveItemByPrice(
				100.0, 900.0);
		for (Item itemX : itemListByPrice) {
			System.out.println("|  Item Name: " + itemX.getItemName()
					+ "  		|  Item Price: " + itemX.getItemPrice() + "  |");
		}
		System.out.println("-------------------------------------");

		System.out.println("\n----------- Item By Item Details -------");
		List<ItemDetail> itemDetailList = itemManagerService
				.retrieveItemDetails(14L);
		
		for (ItemDetail itemDetailX : itemDetailList) {
			System.out
					.println("|  Item Id: " + itemDetailX.getItemDetailId()
							+ "  		|  Item Total Size: "
							+ itemDetailX.getItemSize() + "			|  Item Extras? "
							+ itemDetailX.getExtras() + "  		|");
		}
		
		
	}

}
